package com.cuitrip.business.tripservice.presenter;

import com.cuitrip.business.login.LoginInstance;
import com.cuitrip.business.tripservice.model.PictureDescription;
import com.cuitrip.business.tripservice.model.ServiceInfo;
import com.cuitrip.business.tripservice.ui.TripEditPicView;
import com.lab.app.BaseFragment;
import com.lab.mvp.presenter.Presenter;
import com.lab.utils.ArrayUtil;
import com.lab.utils.ToastUtil;
import com.lab.utils.imagechooser.EasyImageChooser;
import com.lab.utils.imageupload.imp.QiniuService;
import com.lab.utils.imageupload.model.QiniuTokenObject;

import java.io.File;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * 图片编辑页面presenter
 * Created by ak
 */
public class TripEditPicPresenter implements Presenter<TripEditPicView> {


    private TripEditPicView mTripEditView;

    private Queue<String> waitUploadList = new ConcurrentLinkedQueue<String>();

    private int uploadPosition = 0;
    private int uploadSize = 0;

    private boolean uploadTerminal = false;

    private ServiceInfo mServiceInfo;


    @Override
    public void attachView(TripEditPicView view) {
        mTripEditView = view;
    }

    @Override
    public void detachView() {
        mTripEditView = null;
    }

    public void initServiceInfo(ServiceInfo serviceInfo) {
        this.mServiceInfo = serviceInfo;
    }

    public void addPhoto(BaseFragment fragment, int code) {
        EasyImageChooser.chooseImage(fragment, code, mServiceInfo.getPictureDescriptionList() == null ? 0 : mServiceInfo.getPictureDescriptionList().size());
    }

    private boolean needUpload() {
        return waitUploadList.size() > 0;
    }

    public void appendUploadList(List<String> pathList) {
        if (!ArrayUtil.isNullOrEmpty(pathList)) {
            for (String path : pathList) {
                waitUploadList.add(path);
            }
        }
    }

    public void appendUploadPath(String path) {
        waitUploadList.add(path);
    }

    private void upload() {
        QiniuService.INSTANCE.requestTokenObject(new QiniuService.RequestQiniuTokenCallback() {
            @Override
            public void onSuccess(QiniuTokenObject qiniuTokenObject) {
                startUpload();
            }

            @Override
            public void onFailed(String errorMessage) {
                ToastUtil.toast(errorMessage);
            }
        });
    }

    private void startUpload() {
        uploadPosition = 0;
        uploadSize = waitUploadList.size();
        uploadLoopAsync();
    }


    private void uploadLoopAsync() {
        if (waitUploadList.peek() != null) {
            mTripEditView.showUploading(uploadPosition + 1, uploadSize, 0);
            String waitObject = waitUploadList.poll();
            File photoFile = new File(waitObject);
            QiniuService.INSTANCE.asyncPutImage(QiniuService.getObjectName(LoginInstance.getInstance().getUserInfo().getUid()), photoFile.getAbsolutePath(), new QiniuService.UploadCallback() {
                @Override
                public void onFailure(String errorMessage) {
                    mTripEditView.hideUploading();
                    uploadPosition++;
                }

                @Override
                public void onSuccess(String url) {
                    PictureDescription pictureDescription = new PictureDescription();
                    pictureDescription.setUrl(url);
                    mTripEditView.addPictureDescription(pictureDescription);
                    uploadPosition++;
                    if (waitUploadList.peek() != null && !uploadTerminal) {
                        uploadLoopAsync();
                    } else {
                        mTripEditView.hideUploading();
                    }
                }

                @Override
                public void onProgress(int progress) {
                    mTripEditView.showUploading(uploadPosition + 1, uploadSize, progress);
                }
            });
        }
    }

    public void uploadPicList() {
        if (needUpload()) {
            upload();
        }
    }

    public boolean cancelUpload() {
        QiniuService.INSTANCE.cancelUpload();
        mTripEditView.hideUploading();
        return true;
    }


}
